import { warnEventId } from '../../../../../common/warnId';
import warningCheck from '../warningCheck';
import responseValidate from '../responseValidate';

jest.mock('../../../../../common/warnId', () => ({
	__esModule: true,
	default: 'mockedDefaultExport',
	warnEventId: jest.fn(({ id }) => id),
}));

test('check http response', () => {
	const response = responseValidate({
		tid: 'e2e8d68fe593436cb61e10a1a7b06b08',
		resp_avg: 120000,
		detail: [
			{
				area: '北京市移动',
				ip: '',
				resp_time: 120000,
				download_time: 120000,
				download_speed: 0.01,
				status_code: 0,
				dns_time: 0,
			},
			{
				area: '云浮市电信',
				ip: '',
				resp_time: 120000,
				download_time: 120000,
				download_speed: 0.01,
				status_code: 0,
				dns_time: 0,
			},
			{
				area: '烟台市电信',
				ip: '',
				resp_time: 120000,
				download_time: 120000,
				download_speed: 0.01,
				status_code: 0,
				dns_time: 0,
			},
			{
				area: '西安市电信2',
				ip: '',
				resp_time: 120000,
				download_time: 120000,
				download_speed: 0.01,
				status_code: 0,
				dns_time: 0,
			},
			{
				area: '成都市移动',
				ip: '',
				resp_time: 120000,
				download_time: 120000,
				download_speed: 0.01,
				status_code: 0,
				dns_time: 0,
			},
		],
	});
	const rules = {
		respTime: 10000,
		nodeCount: 4,
		statusCodeRange: [[500, 599]],
	};

	const warnings = warningCheck({ response, rules });
	expect(warnings).toEqual({
		warnings: [
			{
				level: 1,
				id: warnEventId({ taskType: 'http', id: 'respTime' }),
				title: '服务器响应时间超过 10000ms',
				detail: {
					area: [
						'北京市移动',
						'云浮市电信',
						'烟台市电信',
						'西安市电信2',
						'成都市移动',
					],
					node: [
						'beijing-cmc',
						'yunfu-ctc',
						'yantai-ctc',
						'tx-xian',
						'chengdu-cmc',
					],
				},
			},
		],
		addition: {
			tid: 'e2e8d68fe593436cb61e10a1a7b06b08',
			respAvg: 120000,
			detail: [
				{
					securityStatus: 'warning',
					warnings: [warnEventId({ taskType: 'http', id: 'respTime' })],
					area: '北京市移动',
					node: 'beijing-cmc',
					ip: '',
					downloadTime: 120000,
					downloadSpeed: 0.01,
					statusCode: 0,
					dnsTime: 0,
					totalTime: 120000,
					response: '',
				},
				{
					securityStatus: 'warning',
					warnings: [warnEventId({ taskType: 'http', id: 'respTime' })],
					area: '云浮市电信',
					node: 'yunfu-ctc',
					ip: '',
					downloadTime: 120000,
					downloadSpeed: 0.01,
					statusCode: 0,
					dnsTime: 0,
					totalTime: 120000,
					response: '',
				},
				{
					securityStatus: 'warning',
					warnings: [warnEventId({ taskType: 'http', id: 'respTime' })],
					area: '烟台市电信',
					node: 'yantai-ctc',
					ip: '',
					downloadTime: 120000,
					downloadSpeed: 0.01,
					statusCode: 0,
					dnsTime: 0,
					totalTime: 120000,
					response: '',
				},
				{
					securityStatus: 'warning',
					warnings: [warnEventId({ taskType: 'http', id: 'respTime' })],
					area: '西安市电信2',
					node: 'tx-xian',
					ip: '',
					downloadTime: 120000,
					downloadSpeed: 0.01,
					statusCode: 0,
					dnsTime: 0,
					totalTime: 120000,
					response: '',
				},
				{
					securityStatus: 'warning',
					warnings: [warnEventId({ taskType: 'http', id: 'respTime' })],
					area: '成都市移动',
					node: 'chengdu-cmc',
					ip: '',
					downloadTime: 120000,
					downloadSpeed: 0.01,
					statusCode: 0,
					dnsTime: 0,
					totalTime: 120000,
					response: '',
				},
			],
		},
	});
});
